package wortubung.inflection;

import static wortubung.util.CommonUtils.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;

import wortubung.JSONConsts;
import wortubung.core.LANG;
import wortubung.util.TransliterateException;

public class IrregularVerbCache {
	private Map<String,VerbConjugation> irregularConjugations;
	private Map<String,String[]> sufficesOf4;

	public IrregularVerbCache() {
		irregularConjugations = new HashMap<String,VerbConjugation>();
		sufficesOf4 = new HashMap<String,String[]>();
	}

	public void readFromJSON(String jsonFile, LANG lang) throws IOException, JSONException, TransliterateException {
		String jsonStr = readFileFromCPAsString(jsonFile);
		int idx = jsonStr.indexOf(JSONConsts.__CUT__.name());
		if (idx > 0)
			jsonStr = jsonStr.substring(0, idx);
		JSONArray jsonArr = new JSONArray(jsonStr);
		int i;
		for (i=0; i<jsonArr.length(); ++i) {
			ConjugationResult result = ConjugationResult.fromJSON(jsonArr.getJSONObject(i), lang).getConjugationResult(null);
			addIrregularConjugation(result);
		}
	}

	public void addIrregularConjugation(VerbConjugation conj) {
		if (conj == null)
			return;
		String verb = conj.getVerb();
		if (!irregularConjugations.containsKey(verb)) {
			irregularConjugations.put(verb, conj);
			addSufficesOf4(verb);
		}
	}

	public VerbConjugation getIrregularConjugation(String verb) {
		return irregularConjugations.get(verb);
	}

	public void removeIrregularConjugation(String verb) {
		irregularConjugations.remove(verb);
	}

	public boolean contains(String verb) {
		return irregularConjugations.containsKey(verb);
	}

	public Collection<String> getVerbs() {
		return irregularConjugations.keySet();
	}

	public Collection<VerbConjugation> getConjugations(boolean sorted, LANG lang) {
		if (!sorted)
			return irregularConjugations.values();

		HashMap<String, String> deAccented = new HashMap<String, String>();
		String[] sa = new String[irregularConjugations.size()];
		int i = 0;
		for (String verb : getVerbs())
			deAccented.put(sa[i++] = lang.toLatin(verb, false), verb);
		Arrays.sort(sa);
		ArrayList<VerbConjugation> ret = new ArrayList<VerbConjugation>();
		for (String dav : sa)
			ret.add(irregularConjugations.get(deAccented.get(dav)));
		return ret;
	}

	public void addSufficesOf4(String verbeSuffix) {
		String racine = last(verbeSuffix, 4);
		String[] sa = sufficesOf4.get(racine);
		if (sa == null) {
			sufficesOf4.put(racine, new String[]{ verbeSuffix });
		} else {
			for (String s : sa)
				if (s.equals(verbeSuffix))
					return;
			sa = Arrays.copyOf(sa, sa.length + 1);
			sa[sa.length - 1] = verbeSuffix;
			sortByLength(sa);
			sufficesOf4.put(racine, sa);
		}
	}

	public String getSuffix(String verb) {
		String racine = last(verb, 4);
		String[] sa = sufficesOf4.get(racine);
		if (sa == null)
			return null;
		for (String s : sa)
			if (verb.endsWith(s))
				return s;
		return null;
	}
}
